package com.xander.qqdatabase.controller;

import com.xander.qqdatabase.util.VerifyCodeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 叶振东
 * @date 2019-01-10
 */
@Controller
public class HomeController {
    @RequestMapping("/")
    public String toIndex() {
        return "index";
    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @GetMapping("login")
    public String login() {
        return "login";
    }

    @GetMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }

    @GetMapping("register")
    public String register() {
        return "register";
    }

    @GetMapping("log")
    public String log() {
        return "log";
    }

    @GetMapping("captcha")
    @ResponseBody
    public void captcha(HttpSession session, HttpServletResponse response) throws Exception {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        String code = VerifyCodeUtils.generateVerifyCode(4);
        session.removeAttribute("captcha");
        session.setAttribute("captcha", code);
        int w = 100, h = 30;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), code);
    }

    @GetMapping("user/userinfo")
    public String userinfo() {
        return "user/userinfo";
    }

    @GetMapping("search/qq")
    public String qq() {
        return "search/qq";
    }

    @GetMapping("search/group")
    public String group() {
        return "search/group";
    }

    @GetMapping("search/quninfo")
    public String quninfo() {
        return "search/quninfo";
    }
}
